package com.thb.push.util;

import org.slf4j.Logger;

import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushTagMessageRequest;
import com.baidu.yun.channel.model.PushTagMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.thb.logs.utils.LogUtils;

public class PushUtils {

	private static final Logger logger = LogUtils.getPushLog();

	public final static String apiKey = "mOAlnEsELEKvfN2RGYUd74I3";
	public final static String secretKey = "FwO3N8crEBCnP2ALlgiGgbfgVOH1Tbyu";

	public static void sendTagMessage(String tag, String message) {
		/*
		 * @brief 推送组播消息(消息类型为透传，由开发方应用自己来解析消息内容) message_type = 0 (默认为0)
		 */

		// 1. 设置developer平台的ApiKey/SecretKey
		// String apiKey = "so0E3YFpKtDfFCePeHofULWA";
		// String secretKey = "vOgN90sWZkQniDo7Nbe4kEXAWB9eAYxy";
		ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

		// 2. 创建BaiduChannelClient对象实例
		BaiduChannelClient channelClient = new BaiduChannelClient(pair);

		// 3. 若要了解交互细节，请注册YunLogHandler类
		channelClient.setChannelLogHandler(new YunLogHandler() {
			@Override
			public void onHandle(YunLogEvent event) {
				logger.info(event.getMessage());
			}
		});

		try {

			// 4. 创建请求类对象
			PushTagMessageRequest request = new PushTagMessageRequest();
			request.setDeviceType(3); // device_type => 1: web 2: pc 3:android
										// 4:ios 5:wp
			request.setTagName(tag);
			request.setMessage(message);
			// 若要通知，
			request.setMessageType(1);
			request.setMessage("{\"title\":\"贴海报通知\",\"description\":\""
					+ message + "\"}");

			// 5. 调用pushMessage接口
			PushTagMessageResponse response = channelClient
					.pushTagMessage(request);

			// 6. 认证推送成功
			logger.info("push amount : " + response.getSuccessAmount());

		} catch (ChannelClientException e) {
			// 处理客户端错误异常
			e.printStackTrace();
		} catch (ChannelServerException e) {
			// 处理服务端错误异常
			logger.info(String.format(
					"request_id: %d, error_code: %d, error_message: %s",
					e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
		}
	}

}
